// DESC: This file provide an example of write some Chinese Characters and English Characters
// to pdf page and then output as multiple pages pdf
// More infomation about configuration: grep `TODO` in this file

// TODO: compile this file with, test passed on debian 12 system
// deps: sudo apt install libhpdf-dev
// g++ <this file> -std=c++17 -hpdf -lz -lpng -ggdb
#include <hpdf.h>
#include <cstdlib>
#include <cstdio>
#include <cassert>

// I am not using Windows Platform
// #ifdef _WIN32
// #include <windows.h>
// #endif

void error_handler(HPDF_STATUS error_no, HPDF_STATUS detail_no, void *user_data)
{
    HPDF_UNUSED(user_data);
    // TODO: when error code occurs, goto https://kimi.ai to ask why
    fprintf(stderr, "ERROR: error_no=%04X, detail_no=%u\n", (HPDF_UINT)error_no, (HPDF_UINT)detail_no);
}

// TODO: define your own sample text
#define SAMPLE_TEXT_TO_WRITE_IN_PDF "错误代码,goto: https:kimi.ai"
// TODO: define your own output pdf file path with filename
#define OUTPUT_PDF "multiple_pages.pdf"

int main()
{
    HPDF_Doc pdf = HPDF_New(error_handler, NULL);
    assert(pdf);

    // if (!pdf)
    //{
    //     fprintf(stderr, "Cannot create PDF object.\n");
    //     return 1;
    // }

    // TODO: important to write Chinese or fix some error code here
    HPDF_SetCompressionMode(pdf, HPDF_COMP_ALL);
    HPDF_UseUTFEncodings(pdf);
    HPDF_SetCurrentEncoder(pdf, "UTF-8");
    HPDF_UseCNSFonts(pdf);

    // Use a standard font, by default, Helvetica is embeded in Pdf format
    // HPDF_Font font = HPDF_GetFont(pdf, "Helvetica", "UTF-8");

    // TODO: I am using my own provided kaiti.ttf file
    const char *font_name = HPDF_LoadTTFontFromFile(pdf, "./kaiti.ttf", HPDF_TRUE);
    assert(font_name);
    // auto font = HPDF_GetTTFontDefFromFile(pdf, "./kaiti.ttf", HPDF_TRUE);
    HPDF_Font font = HPDF_GetFont(pdf, font_name, "UTF-8");
    assert(font != nullptr);

    // Define the number of characters per page and per line
    int charsPerPage = 300;
    int charsPerLine = 15;
    int linesPerPage = charsPerPage / charsPerLine;

    // Initialize the current position
    HPDF_Page page = HPDF_AddPage(pdf);
    HPDF_Page_SetFontAndSize(page, font, 12);
    HPDF_REAL y = 750; // Start from the top of the page

    // Initialize the total number of characters written
    int totalCharsWritten = 0;

    // Write characters to the PDF
    while (totalCharsWritten < charsPerPage * 100)
    { // Write 100 pages for example
        HPDF_Page_BeginText(page);
        for (int line = 0; line < linesPerPage; line++)
        {
            char text[16]; // Buffer for 15 characters + null terminator
            for (int i = 0; i < charsPerLine; i++)
            {
                text[i] = 'A' + (i % 26); // Simple pattern of characters
            }
            text[charsPerLine] = '\0'; // Null-terminate the string

            // Calculate x position based on the length of the text
            HPDF_Page_TextWidth(page, text);                                                // Calculate the width of the text
            HPDF_REAL x = (HPDF_Page_GetWidth(page) - HPDF_Page_TextWidth(page, text)) / 2; // Center the text

            // Output the text
            // TODO: I dont want to write English Characters
            // HPDF_Page_TextOut(page, x, y, text);
            HPDF_Page_TextOut(page, x, y, SAMPLE_TEXT_TO_WRITE_IN_PDF);
            y -= 20; // Move to the next line

            // If we've reached the bottom of the page, start a new page
            if (y < 50)
            {
                HPDF_Page_EndText(page);
                page = HPDF_AddPage(pdf);
                HPDF_Page_SetFontAndSize(page, font, 12);
                y = 750; // Reset y position for the new page
                HPDF_Page_BeginText(page);
            }
        }
        HPDF_Page_EndText(page);
        totalCharsWritten += charsPerPage; // Increment the total characters written
    }

    // Save the document to a file
    HPDF_SaveToFile(pdf, OUTPUT_PDF);

    // Clean up
    HPDF_Free(pdf);

    return 0;
}
